//
//  MSSubFilterTableViewController.m
//  JYG_Agent
//
//  Created by apple on 15/10/19.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MSSubFilterTableViewController.h"

@interface MSSubFilterTableViewController ()
@end

@implementation MSSubFilterTableViewController
@synthesize dataArray,curIndexPath;

-(void)setDataArray:(NSArray*)array
{
    dataArray=[[NSMutableArray alloc] initWithArray:array];
    [self.tableView reloadData];
    [self scrollToSelectedIndex];
}


-(void)scrollToSelectedIndex
{
    int index=0;
    for (int i=0;i<dataArray.count;i++)
    {
        NSObject* item=[dataArray objectAtIndex:i];
        if ([item isKindOfClass:[NSString class]])
        {
            if ([(NSString*)item isEqualToString:(NSString*)self.selected])
            {
                index=i;
            }
        }
    }
    [self.tableView scrollRectToVisible:CGRectMake(0, self.cellHeight*(index-1), APPFRAME.size.width, 280) animated:NO];
}

- (void)dismissSubViews
{
    self.curIndexPath = nil;
    [self.thirdLayerFilter.view removeFromSuperview];
    [self.view removeFromSuperview];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* indentifer=@"indentiferCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    
    if (cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
        cell.tintColor=kBlueColor;
    }
    cell.accessoryType=UITableViewCellAccessoryNone;
    cell.textLabel.textColor=kDarkGrayColor;
    cell.backgroundColor=[UIColor clearColor];
    
    NSObject* obj=[self.dataArray objectAtIndex:indexPath.row];
    if ([obj isKindOfClass:[NSString class]])
    {
        [_thirdLayerFilter.view removeFromSuperview];
        cell.textLabel.text=[self.dataArray objectAtIndex:indexPath.row];
        if ([cell.textLabel.text isEqualToString:(NSString*)self.selected])
        {
            cell.textLabel.textColor=kBlueColor;
        }
    }
    else if ([obj isKindOfClass:[NSDictionary class]])
    {
        NSDictionary* item=(NSDictionary*)obj;
        NSString* key=[[item allKeys] firstObject];
        NSArray* subItems=[item objectForKey:key];
        
        if ((curIndexPath!=nil&&(indexPath.section==curIndexPath.section&&indexPath.row==curIndexPath.row))
            ||(self.selected!=nil&&([key isEqualToString:(NSString*)self.selected]||[subItems containsObject:self.selected])&&curIndexPath==nil))
        {
            if (!self.thirdLayerFilter.view.superview) {
                [self.view.superview addSubview:self.thirdLayerFilter.view];
            }
            cell.backgroundColor=[UIColor whiteColor];
            self.thirdLayerFilter.selected=self.selected;
            if ([key isEqualToString:(NSString*)self.selected]) {
                self.thirdLayerFilter.selected = @"不限";
            }
            self.thirdLayerFilter.dataArray=(NSMutableArray*)subItems;
            self.thirdLayerFilter.view.frame=CGRectMake(APPFRAME.size.width*2/3.0, -1, APPFRAME.size.width/3.0+1, self.view.height+1);
            self.thirdLayerFilter.view.hidden=NO;
            self.selectedKey=key;
            curIndexPath=indexPath;
            cell.textLabel.textColor=kBlueColor;
        }
        
        cell.textLabel.text=key;
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==self.tableView)
    {
        NSObject* obj=[self.dataArray objectAtIndex:indexPath.row];
        if ([obj isKindOfClass:[NSDictionary class]])
        {
            curIndexPath=indexPath;
            [tableView reloadData];
            return;
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView==self.thirdLayerFilter.tableView)
    {
        self.selected=self.thirdLayerFilter.selected;
        [self.tableView reloadData];
    }
    else
    {
        self.selected=[self.dataArray objectAtIndex:indexPath.row];
    }
    if ([(NSString*)self.selected isEqualToString:@"不限"])
    {
        self.selected=self.selectedKey;
    }
    
    if ([self.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
        [self.delegate performSelector:@selector(tableView:didSelectRowAtIndexPath:) withObject:tableView withObject:indexPath];
    }
}

-(MSThirdLayerFilterViewController*)thirdLayerFilter
{
    if (_thirdLayerFilter==nil)
    {
        _thirdLayerFilter=[[MSThirdLayerFilterViewController alloc]initWithStyle:UITableViewStylePlain];
        _thirdLayerFilter.delegate=(id)self;
        _thirdLayerFilter.view.backgroundColor=[UIColor whiteColor];
        _thirdLayerFilter.view.frame=CGRectMake(APPFRAME.size.width*2/3.0, -1, APPFRAME.size.width/3.0+1, self.view.height+1);
        _thirdLayerFilter.enableRefresh=NO;
        _thirdLayerFilter.cellHeight=40.0;
        _thirdLayerFilter.tableView.bounces=NO;
        _thirdLayerFilter.view.layer.shadowColor = [UIColor blackColor].CGColor;
        _thirdLayerFilter.view.layer.shadowOffset = CGSizeMake(0.5, -0.5);
        _thirdLayerFilter.view.layer.shadowOpacity = 0.5;
        _thirdLayerFilter.view.layer.borderWidth = 1.5;
        _thirdLayerFilter.view.layer.borderColor = kDefaultViewBackgroundColor.CGColor;
    }
    return _thirdLayerFilter;
}

@end
